/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuadradogenetico;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jgap.Gene;
import org.jgap.IChromosome;

/**
 *
 * @author Luis Roberto
 */
public class App extends javax.swing.JFrame {

    /**
     * Creates new form App
     */
    private Algoritmo ag;
    private DefaultTableModel modelo;
    public App() {
        initComponents();
        ag = new Algoritmo();
        modelo = new DefaultTableModel();
        tabla.setModel(modelo);
        modelo.addColumn("No");
        modelo.addColumn("Generacion");
        modelo.addColumn("Edad");
        modelo.addColumn("Sujeto");
        modelo.addColumn("Adaptacion");
        agregarGeneracion();
    }
    
    private void agregarGeneracion(){
        Object fila[] = new Object[5];
        List<IChromosome> cromosomas = ag.getPoblacion().getChromosomes();
        int lugar = 0;
        for (IChromosome c : cromosomas) {
            fila[0] = ++lugar;
            fila[1] = ag.getGeneracion();
            fila[2] = c.getAge();
            fila[3] = verCromosoma(c.getGenes());
            fila[4] = c.getFitnessValue();
            modelo.addRow(fila);
        }
    }
    
    private String verCromosoma(Gene [] genes){
        String retorno = "";
        for (Gene gene : genes) {
            retorno += gene.getAllele().toString();
        }
        return retorno;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("f(x) = x^2 \"Genetico\"");

        jButton1.setText("Evolucionar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setViewportView(tabla);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        ag.evolucionar();
        agregarGeneracion();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new App().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables
}
